package grid;

import stegru.tools.multiThreat.ParallelFor;
public class QGridGenerator {

	private QGridGenerator() {
	}
	
	
	public static Grid generateQGrid(final int numGridPointsX,final int numGridPointsY,
			final int width,final int height, final boolean glueBounds){
		if (glueBounds) {
			return generateGluedQGrid(numGridPointsX, numGridPointsY, width, height);
		}else{
			return generateUngluedQGrid(numGridPointsX, numGridPointsY,width, height);
		}
	}
	
	
	
	
	public static Grid generateUngluedQGrid(final int numGridPointsX,final int numGridPointsY,
			 final int width,final int height){
		final GridBuilder gBuilder = new GridBuilder(numGridPointsX * numGridPointsY);
		final double[] yCoords = Distribution.distribute(numGridPointsY, height);
		final double[] xCoords = Distribution.distribute(numGridPointsX, width);
		(new ParallelFor(0, numGridPointsX - 1) {
			@Override
			protected void loop(int x) {
				for (int y = 0; y < numGridPointsY; y++) {
						gBuilder.set(x + numGridPointsX * y, new GridPoint(
								xCoords[x], yCoords[y]));
					}
			}
		}).calculate();
		return gBuilder.generate();
	}
	
	
	public static Grid generateGluedQGrid(final int numGridPointsX,final int numGridPointsY,
			final double width, final double height){
		final GridBuilder gBuilder = new GridBuilder(numGridPointsX * numGridPointsY);
		final double[] yCoords = Distribution.distribute01(numGridPointsY);
		final double[] xCoords = Distribution.distribute01(numGridPointsX);
		(new ParallelFor(0, numGridPointsX - 1) {
			@Override
			protected void loop(int x) {
				for (int y = 0; y < numGridPointsY; y++) {
					int glue;
					if (xCoords[x] == 0 || xCoords[x] == 1) {
						if (yCoords[y] == 0 || yCoords[y] == 1) {
							glue = GridPoint.TYPE_GLUE_X_Y;
						} else {
							glue = GridPoint.TYPE_GLUE_X;
						}
					} else if (yCoords[y] == 0 || yCoords[y] == 1) {
						glue = GridPoint.TYPE_GLUE_Y;
					} else {
						glue = GridPoint.TYPE_NON_GLUE;
					}
						gBuilder.set(x + numGridPointsX * y, new GridPoint(
								xCoords[x]*width, yCoords[y]*height, glue));					
				}
			}
		}).calculate();
		return gBuilder.generate();
	}

}
